package com.admission.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.communicate.KeyTools;
import com.admission.model.OrderTemp;
import com.admission.model.PayLog;
import com.admission.service.OrderTempManager;
import com.admission.service.PayLogManager;
import com.admission.util.MoneyUtils;
import com.admission.util.PageUtils;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class PayResultAction extends CrudActionSupport<OrderTemp>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5433705528207916713L;
	HttpServletRequest request=ServletActionContext.getRequest();
	private PayLogManager payLogManager;
	private Page<OrderTemp> page = new Page<OrderTemp>(PageUtils.DEFAULT_PAGE_SIZE);
	private OrderTempManager orderTempManager;
	private OrderTemp orderTemp;
	private String MerId;
	private String OrdId;
	private String OrdAmt;
	private String PayStat;
	private String PayTime;
	private String ChkValue;
	private String PayStatMsg;
	private String RMB;

	public PayLogManager getPayLogManager() {
		return payLogManager;
	}

	public void setPayLogManager(PayLogManager payLogManager) {
		this.payLogManager = payLogManager;
	}

	public Page<OrderTemp> getPage() {
		return page;
	}

	public void setPage(Page<OrderTemp> page) {
		this.page = page;
	}

	public OrderTempManager getOrderTempManager() {
		return orderTempManager;
	}

	public void setOrderTempManager(OrderTempManager orderTempManager) {
		this.orderTempManager = orderTempManager;
	}
	public OrderTemp getOrderTemp() {
		return orderTemp;
	}

	public void setOrderTemp(OrderTemp orderTemp) {
		this.orderTemp = orderTemp;
	}


	public String getMerId() {
		return MerId;
	}

	public void setMerId(String merId) {
		MerId = merId;
	}

	public String getOrdId() {
		return OrdId;
	}

	public void setOrdId(String ordId) {
		OrdId = ordId;
	}

	public String getOrdAmt() {
		return OrdAmt;
	}

	public void setOrdAmt(String ordAmt) {
		OrdAmt = ordAmt;
	}

	public String getPayStat() {
		return PayStat;
	}

	public void setPayStat(String payStat) {
		PayStat = payStat;
	}

	public String getPayTime() {
		return PayTime;
	}

	public void setPayTime(String payTime) {
		PayTime = payTime;
	}

	public String getChkValue() {
		return ChkValue;
	}

	public void setChkValue(String chkValue) {
		ChkValue = chkValue;
	}
	
	public String getPayStatMsg() {
		return PayStatMsg;
	}

	public void setPayStatMsg(String payStatMsg) {
		PayStatMsg = payStatMsg;
	}

	public String getRMB() {
		return RMB;
	}

	public void setRMB(String rMB) {
		RMB = rMB;
	}


	public String CheckPayResult()throws Exception {
		if(!PayStat.equals("1001")){
			setPayStatMsg("支付失败");
			return this.list();
		}
		String plainData = request.getSession().getAttribute("PLAINDATA").toString();
		String flag = KeyTools.CheckReturnToken(MerId, plainData, ChkValue);
//		flag = "success";
		if(flag.equals("success")){
			List<PropertyFilter> filters = PropertyFilter
			.buildFromHttpRequest(Struts2Utils.getRequest());
			if (!StringUtils.isEmpty(OrdId)) {
				PropertyFilter p1 = new PropertyFilter("EQS_orderId",
						OrdId);
				filters.add(p1);
			}
			List<OrderTemp> orderTempList = orderTempManager.getPageByFilter(page, filters).getResult();
			for(OrderTemp orderTemp : orderTempList){
				String idNum = orderTemp.getIdnum();
				payLogManager.UpdatePayStatus(idNum, "1", PayTime);
			}
			
			setPayStatMsg("支付成功");
			setRMB(MoneyUtils.stringToMoney(getOrdAmt()));
		}
		else{
			setPayStatMsg("验证签名失败");
		}
		return this.list();
	}
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return "pay-result";
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public OrderTemp getModel() {
		// TODO Auto-generated method stub
		return null;
	}

}
